using System;
using System.Collections.Generic;

/// <summary>
/// 双端队列
/// 用于时间倒带功能
/// </summary>
/// <typeparam name="T">存放的数据类型</typeparam>
public class DoubleEndedQueue<T>
{
    private LinkedList<T> items;

    // 构造函数
    public DoubleEndedQueue()
    {
        items = new LinkedList<T>();
    }

    // 添加队列数据 从对头
    public void AddFirst(T item)
    {
        items.AddFirst(item);
    }

    // 添加队列数据 从队尾
    public void AddLast(T item)
    {
        items.AddLast(item);
    }

    // 获取并删除元素 从队头
    public T GetFirst()
    {
        if(items.Count == 0)
            return default(T);
            // throw new InvalidOperationException("队列为空");

        T first = items.First.Value;
        items.RemoveFirst();
        return first;
    }
    
    // 获取并删除元素 从队尾
    public T GetLast()
    {
        if(items.Count == 0)
            return default(T);
            // throw new InvalidOperationException("队列为空");

        T last = items.Last.Value;
        items.RemoveLast();
        return last;
    }

    // 获取队头元素
    public T PeekFirst()
    {
        if(items.Count == 0)
            return default(T);
            // throw new InvalidOperationException("队列为空");
        T first = items.First.Value;
        return first;
    }

    // 获取队尾元素
    public T PeekLast()
    {
        if(items.Count == 0)
            return default(T);
            // throw new InvalidOperationException("队列为空");
        T last = items.Last.Value;
        return last;
    }

    // 仅删除队头元素
    public void RemoveFirst()
    {
        if(items.Count == 0)
            throw new InvalidOperationException("队列为空");
        items.RemoveFirst();
    }

    // 仅删除队尾元素
    public void RemoveLast()
    {
        if(items.Count == 0)
            throw new InvalidOperationException("队列为空");
        items.RemoveLast();
    }

    // 获取双端队列的大小
    public int Count
    {
        get { return items.Count; }
    }

    // 清空双端队列
    public void Clear()
    {
        items.Clear();
    }
}
